package cn.tedu.medicalmanagementsystem.user.controller;

import cn.tedu.medicalmanagementsystem.base.response.JsonResult;
import cn.tedu.medicalmanagementsystem.base.response.StatusCode;
import cn.tedu.medicalmanagementsystem.user.pojo.dto.AppointmentAddParam;
import cn.tedu.medicalmanagementsystem.user.pojo.vo.UserAppointmentVO;
import cn.tedu.medicalmanagementsystem.user.pojo.vo.UserVO;
import cn.tedu.medicalmanagementsystem.user.service.AppointmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/user/appointments")
@Api(tags = "用户预约管理")
public class UserAppointmentController {

    @Autowired
    private AppointmentService appointmentService;

    // 创建预约
    @PostMapping("/book")
    @ApiOperation("创建预约")
    public JsonResult createAppointment(@RequestBody AppointmentAddParam param, HttpSession session) {
        try {
            Long userId = getCurrentUserId(session);
            if (userId == null) {
                return JsonResult.fail(StatusCode.NOT_LOGIN);
            }
            System.out.println("前端传过来的escortId是：========================="+param.getEscortId());
            System.out.println("Received appointment request: " + param); // 添加日志
            JsonResult result = appointmentService.createAppointment(param, userId);
            System.out.println("Service returned: " + result); // 添加日志
            return result;
        } catch (Exception e) {
            System.out.println("endTime是：=========================");
            e.printStackTrace(); // 打印异常堆栈
            return JsonResult.fail(e.getMessage());
        }
    }

    // 获取用户预约列表
    @GetMapping("")
    @ApiOperation("获取用户预约列表")
    public JsonResult<List<UserAppointmentVO>> listAppointments(HttpSession session) {
        Long userId = getCurrentUserId(session);
        if (userId == null) {
            return JsonResult.fail(StatusCode.NOT_LOGIN);
        }
        return appointmentService.getUserAppointments(userId);
    }

    // 获取预约详情
    @GetMapping("/{appointmentId}")
    @ApiOperation("获取预约详情")
    public JsonResult<UserAppointmentVO> getAppointmentDetails(
            @PathVariable("appointmentId") Long appointmentId,
            HttpSession session) {
        Long userId = getCurrentUserId(session);
        if (userId == null) {
            return JsonResult.fail(StatusCode.NOT_LOGIN);
        }
        return appointmentService.getAppointmentDetails(appointmentId);
    }

    // 取消预约
    @PutMapping("/{appointmentId}/cancel")
    @ApiOperation("取消预约")
    public JsonResult cancelAppointment(
            @PathVariable("appointmentId") Long appointmentId,
            HttpSession session) {
        Long userId = getCurrentUserId(session);
        if (userId == null) {
            return JsonResult.fail(StatusCode.NOT_LOGIN);
        }
        return appointmentService.cancelAppointment(appointmentId, userId);
    }

    private Long getCurrentUserId(HttpSession session) {
        Object userObj = session.getAttribute("user");
        if (userObj == null) {
            log.warn("Session 中没有找到用户信息, session id: {}", session.getId());
            return null;
        }
        return ((UserVO) userObj).getId();
    }
}